package com.qdsgjsfk.vision.model;

import android.util.Xml;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.DecimalFormat;

import okhttp3.MediaType;

/**
 * 拼接json数据，发送指令给美沃验光仪
 *
 * @time 2020/12/14 15:18
 * Created by lbj on 2020/12/14 15:18.
 */
public class BlueToothDataJson {
    private static final MediaType JSON = MediaType.parse( "application/json; charset=utf-8" );
    private static String strUTF8;

    public static String getSendDataOne() {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put( "bleautosend", "1" );
            jsonObject.put( "cmd", "setBleAuto" );
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return "____" + jsonObject.toString() + "====";
    }

    public static String getSendData(String studentId, String studentName, String stuSex, String stuBirth) {
        JSONObject jsonObject = new JSONObject();
        JSONObject jo = new JSONObject();
        System.out.println( "学生姓名---" + studentName );
        try {
            strUTF8 = new String(studentName.getBytes( StandardCharsets.UTF_8 ), StandardCharsets.ISO_8859_1 );
//            strUTF8 = new String( studentName.getBytes( StandardCharsets.UTF_8 ), StandardCharsets.UTF_8 );
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            jo.put( "pid", studentId );
            jo.put( "firstname", studentName );
            jo.put( "lastname", "" );
            jo.put( "sex", "男".equals( stuSex ) ? "M" : "F" );
            jo.put( "birthday", stuBirth );
            jo.put( "glass", "1" );
            jo.put( "reportOD", "" );
            jo.put( "reportOS", "" );
            jo.put( "reportBino", "" );
            jo.put( "diagnosed", "0" );
            jo.put( "uploadToServer", "0" );
            jsonObject.put( "cmd", "addPatient" );
            jsonObject.put( "body", jo );
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return "____" + jsonObject.toString() + "====";
    }

    public static String showReceiveDataAxisOS(String msg) {
        //{"msg":"OK","OS":["2018","0","0","0","126","-15","-180","-187"],"OD":["1978","0","-1","1","28","-26","-149","-162"],"BINO":["14","7251"]}
        //后面几位：轴位126，柱镜度数 DC， 球镜度数 DS，等效球镜度数 SE
        String axisOS = null;
        try {
            JSONObject jsonObject = new JSONObject( msg );
            JSONArray os = jsonObject.getJSONArray( "OS" );
            String AxisOS = os.getString( 4 );
            String DCOS = os.getString( 5 );
            String DSOS = os.getString( 6 );
            axisOS = os.getString( 4 );
            System.out.println( "接收的美沃数据---" + axisOS );
        } catch (JSONException e) {
            e.printStackTrace();

        }
        return axisOS;
    }

    public static String showReceiveDataDCOS(String msg) {
        //{"msg":"OK","OS":["2018","0","0","0","126","-15","-180","-187"],"OD":["1978","0","-1","1","28","-26","-149","-162"],"BINO":["14","7251"]}
        //后面几位：轴位126，柱镜度数 DC， 球镜度数 DS，等效球镜度数 SE
        String dcOS = null;
        try {
            JSONObject jsonObject = new JSONObject( msg );
            JSONArray os = jsonObject.getJSONArray( "OS" );
            String AxisOS = os.getString( 4 );
            String DCOS = os.getString( 5 );
            String DSOS = os.getString( 6 );
            dcOS = formatDouble( DCOS );
            System.out.println( "接收的美沃数据---" + dcOS );
        } catch (JSONException e) {
            e.printStackTrace();

        }
        return dcOS;
    }

    /**
     * 美沃左眼球镜
     *
     * @time 2020/12/15 10:39
     */
    public static String showReceiveDataDSOS(String msg) {
        //{"msg":"OK","OS":["2018","0","0","0","126","-15","-180","-187"],"OD":["1978","0","-1","1","28","-26","-149","-162"],"BINO":["14","7251"]}
        //后面几位：轴位126，柱镜度数 DC， 球镜度数 DS，等效球镜度数 SE
        String dsOS = null;
        try {
            JSONObject jsonObject = new JSONObject( msg );
            JSONArray os = jsonObject.getJSONArray( "OS" );
            String AxisOS = os.getString( 4 );
            String DCOS = os.getString( 5 );
            String DSOS = os.getString( 6 );
            dsOS = formatDouble( DSOS );
            System.out.println( "接收的美沃数据---" + dsOS );
        } catch (JSONException e) {
            e.printStackTrace();

        }
        return dsOS;
    }

    /**
     * 美沃右眼球镜
     *
     * @time 2020/12/15 10:39
     */
    public static String showReceiveDataDSOD(String msg) {
        //{"msg":"OK","OS":["2018","0","0","0","126","-15","-180","-187"],"OD":["1978","0","-1","1","28","-26","-149","-162"],"BINO":["14","7251"]}
        //后面几位：轴位126，柱镜度数 DC， 球镜度数 DS，等效球镜度数 SE
        String dsOD = null;
        try {
            JSONObject jsonObject = new JSONObject( msg );
            JSONArray od = jsonObject.getJSONArray( "OD" );
            String AxisOD = od.getString( 4 );
            String DCOD = od.getString( 5 );
            String DSOD = od.getString( 6 );
            dsOD = formatDouble( DSOD );
            System.out.println( "接收的美沃数据---" + dsOD );
        } catch (JSONException e) {
            e.printStackTrace();

        }
        return dsOD;
    }

    public static String showReceiveDataDCOD(String msg) {
        //{"msg":"OK","OS":["2018","0","0","0","126","-15","-180","-187"],"OD":["1978","0","-1","1","28","-26","-149","-162"],"BINO":["14","7251"]}
        //后面几位：轴位126，柱镜度数 DC， 球镜度数 DS，等效球镜度数 SE
        String dcOD = null;
        try {
            JSONObject jsonObject = new JSONObject( msg );
            JSONArray od = jsonObject.getJSONArray( "OD" );
            String AxisOD = od.getString( 4 );
            String DCOD = od.getString( 5 );
            String DSOD = od.getString( 6 );
            dcOD = formatDouble( DCOD );
            System.out.println( "接收的美沃数据---" + dcOD );
        } catch (JSONException e) {
            e.printStackTrace();

        }
        return dcOD;
    }

    public static String showReceiveDataAxisOD(String msg) {
        //{"msg":"OK","OS":["2018","0","0","0","126","-15","-180","-187"],"OD":["1978","0","-1","1","28","-26","-149","-162"],"BINO":["14","7251"]}
        //后面几位：轴位126，柱镜度数 DC， 球镜度数 DS，等效球镜度数 SE
        String axisOD = null;
        try {
            JSONObject jsonObject = new JSONObject( msg );
            JSONArray od = jsonObject.getJSONArray( "OD" );
            String AxisOD = od.getString( 4 );
            String DCOD = od.getString( 5 );
            String DSOD = od.getString( 6 );
            axisOD = od.getString( 4 );
            System.out.println( "接收的美沃数据---" + axisOD );
        } catch (JSONException e) {
            e.printStackTrace();

        }
        return axisOD;
    }


    private static String formatDouble(String data) {
        double result;
        Double _gradient = Double.parseDouble( "25" );
        Double abs = Double.parseDouble( Math.abs( Integer.parseInt( data ) ) + "" );
        if (abs % _gradient < _gradient / 2) {
            abs = (abs - (abs % _gradient)) / 100;
        } else {
            abs = (abs - (abs % _gradient) + _gradient) / 100;
        }

        if (Math.abs( abs ) < 0.0001) {
            result = Double.parseDouble( "0.00" );
        } else {
            result = Double.parseDouble( Double.parseDouble( data ) > 0 ? "+" + abs : "-" + abs );
        }

        DecimalFormat df = new DecimalFormat( "0.00" );//此为保留1位小数，若想保留2位小数，则填写#.00  ，以此类推
        String format = df.format( result );
        if (result >= 0) {
            format = "+" + format;
        }
        return format;
    }


}
